import time
import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"
}


def getFromUrl(url: str, index: int, total: int):
    if not url:
        print("空url")
        return

    print(f"正在获取：{url} ({index}/{total})")

    try:
        start_time = time.time()
        response = requests.get(url=url, headers=headers, timeout=120)
        sum_time = time.time() - start_time
        print(f"请求耗时：{sum_time:.2f} 秒")

        response.raise_for_status()  # 处理 HTTP 错误

        soup = BeautifulSoup(response.text, "lxml")
        element = soup.find_all("div", class_="card-body")

        if not element:
            print(f"未找到 'card-body'，可能页面结构有变动: {url}")
            return

        # 解析配方描述
        table_cells = element[0].find_all("td")
        dataDescription = {}

        for i in range(0, len(table_cells), 2):  # 以 2 为步长遍历 td
            key = table_cells[i].get_text(strip=True) if i < len(table_cells) else ""
            value = (
                table_cells[i + 1].get_text(" ", strip=True)
                if i + 1 < len(table_cells)
                else ""
            )
            if key:  # 确保 key 不是空的
                dataDescription[key] = value

        print("解析结果：", dataDescription)

        # 解析成分表
        if len(element) < 2:
            print(f"未找到成分表，跳过解析: {url}")
            return

        table_rows = element[1].find_all("tr")
        if not table_rows or len(table_rows) < 2:
            print(f"成分表为空，跳过解析: {url}")
            return

        table_rows.pop(0)  # 移除表头
        dataComponents = {}

        for row in table_rows:
            temp = row.find_all("td")
            if len(temp) < 5:
                print(f"跳过异常行（数据不足）: {temp}")
                continue

            # 处理可能的 None 值
            ComponentID = (
                temp[0].find("a").get_text(strip=True) if temp[0].find("a") else "N/A"
            )
            latinName = temp[1].get_text(" ", strip=True)
            chineseName = temp[2].get_text(" ", strip=True)
            ComponentQuantity = temp[3].get_text(" ", strip=True)
            Barcode = (
                temp[4].find("a").get_text(" ", strip=True)
                if temp[4].find("a")
                else "N/A"
            )

            dataComponents[ComponentID] = {
                "latinName": latinName,
                "chineseName": chineseName,
                "ComponentQuantity": ComponentQuantity,
                "Barcode": Barcode,
            }

            print(
                f"ComponentID: {ComponentID}, latinName: {latinName}, chineseName: {chineseName}, ComponentQuantity: {ComponentQuantity}, Barcode: {Barcode}"
            )

    except requests.Timeout:
        print(f"请求超时: {url}")
    except requests.RequestException as e:
        print(f"请求失败: {e}")
    except Exception as e:
        print(f"解析异常: {e}")


# 测试调用
url = "https://www.bidd.group/TCMID/tcmf.php?formula=TCMF1"  # 替换为真实 URL
getFromUrl(url, index=1, total=10)
